home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / UNDO.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  3KB  |  165 lines

  1. #include "jam.h"
  2. #include"stdlib.h"
  3. #include "def.h"
  4.  
  5. #define UNDO_ON 
  6.  
  7. /* element of undo history
  8. */
  9. #define INSERT        1
  10. #define UPPER        2
  11. #define LOWER        3
  12. #define TWIDDLE        4
  13. #define KILL            5
  14.  
  15. typedef struct _undo
  16.  {
  17.    int type;
  18.    RSIZE line;            /* integer line */
  19.    RSIZE offset;        /* offset within line */
  20.    RSIZE size;            /* size of insert */
  21.    REGION region;               /* undo region */
  22.  } Undo;
  23.  
  24. /* undo state
  25. */
  26. static void rn_(Cache,(RSIZE count));
  27. static Undo rn_(*updateundo,(BUFFER *bp));
  28.  
  29. static Undo *updateundo(bp)
  30. BUFFER *bp;
  31. {
  32. #ifdef UNDO_ON
  33.   if (!bp->b_undo) 
  34.     bp->b_undo = (void *)calloc(1, sizeof(Undo));
  35.   return((Undo *)bp->b_undo);
  36. #else
  37.   return ((Undo *)0);
  38. #endif
  39. }
  40.  
  41. void clearUndo(bp)
  42. BUFFER *bp;
  43. {
  44. #ifdef UNDO_ON
  45.   if (bp->b_undo)
  46.     {
  47.     free((void *)bp->b_undo);
  48.     bp->b_undo = (void *)0;
  49.     }
  50. #endif
  51. }
  52. static void Cache(count)
  53. RSIZE count;
  54. {
  55. #ifdef UNDO_ON
  56.   register Undo *undo = (Undo *)curbp->b_undo;
  57.  
  58.   undo->offset = curwp->w_doto;
  59.   undo->line = getlinenum(curbp, curwp->w_dotp);
  60.   undo->size = count;
  61. #endif
  62. }
  63. void cacheInsert(count)
  64. RSIZE count;
  65. {
  66. #ifdef UNDO_ON
  67.   register Undo *undo = updateundo(curbp);
  68.  
  69.   undo->type = INSERT;
  70.   Cache(count);
  71. #endif
  72. }
  73. void cacheLower(count)
  74. RSIZE count;
  75. {
  76. #ifdef UNDO_ON
  77.   register Undo *undo = updateundo(curbp);
  78.  
  79.   undo->type = LOWER;
  80.   Cache(count);
  81. #endif
  82. }
  83. void cacheUpper(count)
  84. RSIZE count;
  85. {
  86. #ifdef UNDO_ON
  87.   register Undo *undo = updateundo(curbp);
  88.  
  89.   undo->type = UPPER;
  90.   Cache(count);
  91. #endif
  92. }
  93. void cacheTwiddle()
  94. {
  95. #ifdef UNDO_ON
  96.   register Undo *undo = updateundo(curbp);
  97.  
  98.   undo->type = TWIDDLE;
  99.   Cache((RSIZE) 0);
  100. #endif
  101. }
  102. void cacheKill()
  103. {
  104. /* broken */
  105. }
  106.  
  107. /* undo something
  108. */
  109. doUndo(f,  n)
  110. int f, n;
  111. {
  112.   char *nothing = "Nothing to Undo.";
  113.   register Undo *undo;
  114.  
  115. #ifdef UNDO_ON
  116.   if (!(undo = (Undo *)(curbp->b_undo)))
  117.     {
  118.       clearUndo(curbp);   /* silly now */
  119.       ewprintf(nothing);
  120.     }
  121.   else
  122.     {
  123.     /* set dot to correct line
  124.     */
  125.     gotobigline(undo->line);
  126.     curwp->w_doto = (int)undo->offset;
  127.     curwp->w_flag |= WFMOVE|WFEDIT;
  128.     switch(undo->type)
  129.     {
  130.     case INSERT:
  131.         clearUndo(curbp);
  132.         kdelete(); /* clear kill buffer */
  133.         ldelete(undo->size, KFORW); 
  134.     break;
  135.     case UPPER:
  136.         clearUndo(curbp);
  137.     lowerword(FFRAND, (int)undo->size);
  138.     break;
  139.     case LOWER:
  140.         clearUndo(curbp);
  141.     upperword(FFRAND, (int)undo->size);
  142.     break;
  143.     case TWIDDLE:
  144.     twiddle(FFRAND, 0);
  145.         clearUndo(curbp);
  146.     break;
  147.     case KILL:
  148.         clearUndo(curbp);
  149.         yank(FFRAND, 0);
  150.     break;
  151.     default:
  152.         clearUndo(curbp);
  153.         break;
  154.     }
  155.     }
  156. #else
  157.   ewprintf(nothing);
  158. #endif
  159.   return(TRUE);
  160. }
  161.  
  162.  
  163.  
  164.  
  165.